package com.example.tongue.main;

import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tongue.R;
import com.example.tongue.main.Register.Register;
import com.example.tongue.main.Register.dao.Constant;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = "MainActivity";


    //声明控件

    private Button   logIn,register,contactAdministrator,admin,debugTest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件资源

                    logIn = findViewById(R.id.LogIn);
                    register = findViewById(R.id.Register);
                    contactAdministrator=findViewById(R.id.ContactAdministrator);
                    admin=findViewById(R.id.Administrator);
                    debugTest=findViewById(R.id.DebugTest);

        //为按钮设置监听器
        logIn.setOnClickListener(this);
        register.setOnClickListener(this);
        contactAdministrator.setOnClickListener(this);
        admin.setOnClickListener(this);
        debugTest.setOnClickListener(this);
    }




    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.LogIn:
            {
            //弹出内容设置
            String ok = "登陆成功";
            String fail = "密码或账号错误，请重新登陆";
            LogIn();
            }
            break;

            case R.id.Register:
            {
                startActivity(new Intent(
                        MainActivity.this,
                                      Register.class));
            }
            break;
            case R.id.ContactAdministrator:
            {
                Toast.makeText(MainActivity.this, "请联系15037753749", Toast.LENGTH_SHORT).show();

            }
            break;
            case R.id.Administrator:
            {
                startActivity(new Intent(
                        MainActivity.this,
                        Admin.class));
            }
            break;
            case R.id.DebugTest:{
                Toast.makeText(MainActivity.this,"开始测试",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this,BottomNavDemo.class));
            }


        }

    }
    private void LogIn(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                String userName = ((EditText) findViewById(R.id.UserName)).getText().toString();
                String userPassword = ((EditText) findViewById(R.id.UserPassword)).getText().toString();
                FormBody formBody = new FormBody.Builder()
                        .add("username", userName)
                        .add("password", userPassword)
                        .build();
                Request request = new Request.Builder()
                        .url(Constant.Login)
                        .post(formBody)
                        .build();

                try (Response response = okHttpClient.newCall(request).execute()) {

                //判断能否登录主逻辑
                    InputStream Feedback  = response.body().byteStream();

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();//节点流

                    int len = 0;

                    byte[] buf = new byte[1024];

                    while ((len = Feedback.read(buf)) != -1) {
                        bos.write(buf, 0, len);
                    }
                    byte[] result = bos.toByteArray();
                    Feedback.close();
                    bos.close();
                    String whetherLogIn=new String(result);
                    Looper.prepare();
                    if (whetherLogIn.equals("true")) {
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, BottomNavDemo.class);
                        startActivity(intent);

                    } else {
                        Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

                    }
                    Looper.loop();
                } catch (IOException e) {
                    Looper.prepare();
                    Toast.makeText(MainActivity.this, "服务器未开启，请联系管理员", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                    Looper.loop();

                }
            }
        }).start();
    }
}
